<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* for in方法可以浅拷贝 */
        // var obj = {
        //     id: 1,
        //     name: "andy"
        // }
        // var o = {};
        // for (let k in obj){
        //     // k 是属性名；obj[k]是属性值
        //     o[k] = obj[k]
        // }
        // console.log(o);

        /* 语法糖assign()浅拷贝 */
        // Object.assign(o, obj)
        // console.log(o);

        /* 深拷贝 */
        var obj = {
            id: 1,
            name: "andy",
            msg: {
                age: 18
            }
        };
        var o = {};
        function copy(newObj, oldObj) {
            for (let k in oldObj) {
                item = oldObj[k]
                if (item instanceof Array) {
                    newObj[k] = [];
                    copy(newObj[k], item)
                } else if (item instanceof Object) {
                    newObj[k] = {};
                    copy(newObj[k], item)
                } else {
                    newObj[k] = item
                }
            }
            return o
        }
        copy(o, obj)
        o.msg.age = 2;
        console.log(o);
        console.log(obj);
    </script>
</body>

</html>